﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Field_Add : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //判断用户是否有权限
        UserPower.CheckAction((int)FreeHouse.Power.Actions.TField.Add);
        if (!IsPostBack) BindData();
    }
    protected void save_Click(object sender, EventArgs e)
    {
        error.Items.Clear();
        FreeHouse.Model.TField filed = new FreeHouse.Model.TField();
        filed.ModelId = int.Parse(ModelId.SelectedValue);
        filed.FieldName = FieldName.Text.Trim();
        filed.Description = Server.HtmlEncode(Description.Value.Trim());
        if (filed.ModelId == 0)
            error.Items.Add(new ListItem("选择FieldName 所属实体类"));
        if (filed.FieldName == "")
            error.Items.Add(new ListItem("填写FieldName"));

        if (error.Items.Count == 0)
        {
            FreeHouse.BLL.TField bll = new FreeHouse.BLL.TField();
            if (bll.Exists(filed.ModelId, filed.FieldName))
            {
                error.Items.Add(new ListItem(filed.FieldName + "已存在"));
                return;
            }
            if (bll.Add(filed) > 0)
                error.Items.Add("添加成功.");
        }
        
    }

    protected void BindData()
    {

        ModelId.DataSource = new FreeHouse.BLL.TModel().GetList(100000, "", "ModelId asc");
        ModelId.DataTextField = "ModelName";
        ModelId.DataValueField = "ModelId";
        ModelId.DataBind();
        ModelId.Items.Insert(0, new ListItem("请选择...", "0"));
    }
}
